﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HR_ASP.ViewModel;
using DB.Model;

namespace HR_ASP.Controllers
{
    public class HistoriaController : Controller
    {
        //
        // GET: /Historia/

        public ActionResult Index()
        {
            using (var context = new HREntities())
            {
                return View(context.Historie.Select(n => new HistoriaViewModel()
                {
                    ID = n.ID_Hist,
                    IDDzial = (int)n.ID_Dzial,
                    IDPracow = (int)n.ID_Pracow,
                    IDStan = (int)n.ID_Stan,
                    DataUmowaCzasowa = n.Data_Umowa_Czasowa.HasValue ? (DateTime)n.Data_Umowa_Czasowa : DateTime.Now,
                    DataZatrudnienia = (DateTime)n.Data_Zatrud,
                    DataZwolnienie = n.Data_Zwoln.HasValue ? (DateTime)n.Data_Zwoln : DateTime.Now,
                    Pensja = (decimal)n.Pensja,
                    DataM = (DateTime)n.DataM,
                    DataU = (DateTime)n.DataU

                }).ToArray()
                );
            }
        }

        //
        // GET: /Historia/Details/5

        public ActionResult Details(int id)
        {
            using (var context = new HREntities())
            {
                Historia historiaDB = context.Historie.SingleOrDefault(n => n.ID_Hist == id);
                if (historiaDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new HistoriaViewModel(historiaDB));
            }
        }

        //
        // GET: /Historia/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Historia/Create

        [HttpPost]
        public ActionResult Create(HistoriaViewModel historia)
        {
            try
            {
                using (var context = new HREntities())
                {
                    context.Historie.AddObject(historia.getModel(new Historia()));
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }

        //
        // GET: /Historia/Edit/5

        public ActionResult Edit(int id)
        {
            using (var context = new HREntities())
            {
                Historia historiaDB = context.Historie.SingleOrDefault(n => n.ID_Hist == id);
                if (historiaDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new HistoriaViewModel(historiaDB));
            }
        }

        //
        // POST: /Historia/Edit/5

        [HttpPost]
        public ActionResult Edit(HistoriaViewModel historia)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Historia historiaDB = context.Historie.SingleOrDefault(n => n.ID_Hist == historia.ID);
                    context.ApplyCurrentValues<Historia>("Historie", historia.getModel(historiaDB));
                    context.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Historia/Delete/5

        public ActionResult Delete(int id)
        {
            using (var context = new HREntities())
            {
                Historia historiaDB = context.Historie.SingleOrDefault(n => n.ID_Hist == id);
                if (historiaDB == null)
                {
                    return RedirectToAction("Index");
                }
                return View(new HistoriaViewModel(historiaDB));
            }
        }

        //
        // POST: /Historia/Delete/5

        [HttpPost]
        public ActionResult Delete(HistoriaViewModel historia)
        {
            try
            {
                using (var context = new HREntities())
                {
                    Historia historiaDB = context.Historie.SingleOrDefault(n => n.ID_Hist == historia.ID);
                    if (historiaDB != null)
                    {
                        context.Historie.DeleteObject(historiaDB);
                        context.SaveChanges();
                    }
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View("Error");
            }
        }
    }
}
